package com.chenbei.controller.admin.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenbei.common.annotation.OperationLog;
import com.chenbei.common.util.PaginationUtil;
import com.chenbei.common.util.ApiResponseUtil;
import com.chenbei.common.domain.vo.Pagination;
import com.chenbei.common.domain.vo.ApiResponse;
import com.chenbei.system.domain.SysUser;
import com.chenbei.system.domain.SysRole;
import com.chenbei.system.service.ISysRoleService;
import com.chenbei.system.service.ISysUserRoleService;
import com.chenbei.system.service.impl.SysRoleService;
import com.chenbei.system.vo.RoleBindUsers;
import com.chenbei.system.vo.req.SysRoleBindPermissionReq;
import com.chenbei.system.vo.req.SysRoleReq;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "角色管理")
@RestController
@RequestMapping("/admin/system/role")
public class RoleController {

    @Autowired
    private ISysRoleService adminRoleService;

    @Autowired
    private ISysUserRoleService adminUserRoleRefService;

    @Autowired
    private SysRoleService sysRoleService;

    @SaCheckPermission(value = "system:role:add", orRole = "root")
    @Operation(summary = "添加角色")
    @PostMapping("add")
    @OperationLog(moduleName = "角色管理", eventName = "添加角色")
    public ApiResponse<Object> add(@RequestBody SysRole sysRole) {
        adminRoleService.add(sysRole);
        return ApiResponseUtil.success();
    }

    @SaCheckPermission(value = "system:role:delete", orRole = "root")
    @Operation(summary = "删除角色")
    @OperationLog(moduleName = "角色管理", eventName = "删除角色")
    @PostMapping("deleteById/{id}")
    public ApiResponse<Object> delete(@PathVariable Long id) {
        adminRoleService.delete(id);

        return ApiResponseUtil.success();
    }

    @SaCheckPermission(value = "system:role:update", orRole = "root")
    @Operation(summary = "更新角色")
    @OperationLog(moduleName = "角色管理", eventName = "更新角色")
    @PostMapping("update")
    public ApiResponse<Object> update(@RequestBody SysRole sysRole) {
        adminRoleService.update(sysRole);
        return ApiResponseUtil.success();
    }

    @SaCheckPermission(value = "system:role:list", orRole = "root")
    @Operation(summary = "角色分页列表")
    @OperationLog(moduleName = "角色管理", eventName = "角色列表")
    @GetMapping("page")
    public ApiResponse<Pagination<SysRole>> page(SysRoleReq sysRoleReq) {
        IPage<SysRole> page = adminRoleService.page(sysRoleReq);
        return ApiResponseUtil.success(PaginationUtil.toPagination(page));
    }

    @Operation(summary = "角色列表")
    @GetMapping("list")
    public ApiResponse<List<SysRole>> list(SysRoleReq sysRoleReq) {
        return ApiResponseUtil.success(sysRoleService.list(sysRoleReq));
    }

    @SaCheckPermission(value = "system:role:bindUsers", orRole = "root")
    @Operation(summary = "角色绑定用户")
    @OperationLog(moduleName = "角色管理", eventName = "角色绑定用户")
    @PostMapping("bindUsers")
    public ApiResponse<Object> bindUsers(@RequestBody RoleBindUsers roleBindUsers) {
        adminUserRoleRefService.roleBindUsers(roleBindUsers.getRoleId(), roleBindUsers.getUserIds());

        return ApiResponseUtil.success();
    }

    @SaCheckPermission(value = "system:role:boundUserPageList", orRole = "root")
    @Operation(summary = "角色绑定用户列表")
    @OperationLog(moduleName = "角色管理", eventName = "角色绑定用户列表")
    @GetMapping("boundUserPageList")
    public ApiResponse<Pagination<SysUser>> boundUserPageList(SysRoleReq sysRoleReq) {
        Page<SysUser> sysAdminUserPage = adminUserRoleRefService.boundUsers(sysRoleReq);
        return ApiResponseUtil.success(sysAdminUserPage);
    }

    @SaCheckPermission(value = "system:role:bindPermissions", orRole = "root")
    @Operation(summary = "绑定菜单权限")
    @OperationLog(moduleName = "角色管理", eventName = "绑定菜单权限")
    @PostMapping("bindPermissions")
    public ApiResponse<Boolean> bindPermissions(@RequestBody SysRoleBindPermissionReq roleBindPermissionReq) {
        sysRoleService.bindPermissions(roleBindPermissionReq);
        return ApiResponseUtil.success(true);
    }
}
